package com.qf.index;

import cn.hutool.core.collection.CollUtil;

import java.sql.SQLException;
import java.util.*;
import java.util.regex.Pattern;

public class Unit {

    public static void main(String[] args) throws SQLException {

        String chinaHospTestTable = "scholar_25_01.tb_true_china_hosp_6_year_test";

        Map<String, String> standardMap = new HashMap<>();
        standardMap.put("department medical", "Department Medical");
        Set<String> acadSet = uSet.getAcadSet();
        Set<String> centerSet = uSet.getCenterSet();
        Set<String> collSet = uSet.getCollSet();
        Set<String> hospSet = uSet.getHospSet();
        Set<String> univSet = uSet.getUnivSet();
        Set<String> removeSet = uSet.getRemoveSet();
        Set<String> dellSet = uSet.getDellSet();
        Set<String> deptSet = uSet.getDeptSet();
        // 西南交通大学力学与工程学院, 成都, 四川 610031, 中国
        String full = "Hong Kong, third peoples hospital, Pokfulam, Pokfulam Rd, Hong Kong, Peoples R China";

        Set<String> dept = Dept.getDept(full, deptSet, dellSet, standardMap, "[hosp]");
        Set<String> unit = getEnUnit(full, standardMap, acadSet, centerSet, collSet, hospSet, univSet, removeSet, dellSet);
        System.out.println("dept: " + dept);
        System.out.println("unit: " + unit);

        Set<String> units = new HashSet<>();
        getCnUnit(full, units);
        System.out.println(units);

    }

    public static void getCnUnit(String full, Set<String> unitSet) {
        List<String> univList = cList.getCnUnivList();
        List<String> hospList = cList.getCnHospList();
        Set<String> errCnUnitSet = cList.getCnErrUnitSet();
        // .replaceAll("\\s+(\\d+)", "$1") ->数字前面如果有空格则给予删除  四川大学华西第二医院/华西妇产儿童医院(.replace("/", "|")去掉的原因)
        String[] split = full.replace("|", " ").replace("　", " ").replace("（", "(").replace("）", ")").replaceAll(" +", " ").replace(" (", "(").replace(" )", ")")
                .replace("、", "|").replace("—", "|").replace("!", "|").replace("！", "|").replace("暨", "|")
                .replace("癌症中心·", "癌症中心|").replace("办公室-", "办公室|").replace("保健院·", "保健院|").replace("单位-", "单位|").replace("防治中心·", "防治中心|").replace("港航-", "港航|")
                .replace("规划署-", "规划署|").replace("科学院·", "科学院|").replace("气象局-", "气象局|").replace("实验室-", "实验室|").replace("实验室·", "实验室|").replace("系统所-", "系统所|")
                .replace("学部-", "学部|").replace("学系-", "学系|").replace("学院-", "学院|").replace("研究基地-", "研究基地|").replace("研究所-", "研究所|").replace("研究所·", "研究所|")
                .replace("研究中心·", "研究中心|").replace("医学中心·", "医学中心|").replace("医院-", "医院|").replace("医院·", "医院|").replace("政府-", "政府|").replace("中国·", "中国|")
                .replace(",", "|").replace(".", "|").replace(";", "|").replace(":", "|").replaceAll("\\s+(\\d+)", "$1").replaceAll("\\d{6}", "|").replaceAll("\\d{5}", "|")
                .replace("，", "|").replace("。", "|").replace("；", "|").replace("：", "|").split("\\|");
        for (String string : split) {
            String s = string.trim();
            if (s.length() > 1) {
                String univ = findCnUnit(univList, s, errCnUnitSet);
                String hosp = findCnUnit(hospList, s, errCnUnitSet);
//                System.out.println("univ:" + univ + " hosp:" + hosp);
                if (!univ.isEmpty() && !hosp.isEmpty()) {
                    unitSet.add(univ);
                    unitSet.add(hosp);
                    // 此类为大学加医院的组合体，分别加入大学和医院两部分
                } else if (!univ.isEmpty()) {
                    unitSet.add(univ);
                } else if (!hosp.isEmpty()) {
                    unitSet.add(hosp);
                }
            }
        }
    }

//    public static void getCnUnit(String info, Set<String> unitSet) {
//        List<String> univList = cList.getCnUnivList();
//        List<String> hospList = cList.getCnHospList();
//        Set<String> errCnUnitSet = cList.getCnErrUnitSet();
//        // .replaceAll("\\s+(\\d+)", "$1") ->数字前面如果有空格则给予删除  四川大学华西第二医院/华西妇产儿童医院(.replace("/", "|")去掉的原因)
//        String[] split = info.replace("|", " ").replace("　", " ").replace("（", "(").replace("）", ")").replaceAll(" +", " ").replace(" (", "(").replace(" )", ")")
//                .replace("、", "|").replace("—", "|").replace("!", "|").replace("！", "|").replace("暨", "|")
//                .replace("癌症中心·", "癌症中心|").replace("办公室-", "办公室|").replace("保健院·", "保健院|").replace("单位-", "单位|").replace("防治中心·", "防治中心|").replace("港航-", "港航|")
//                .replace("规划署-", "规划署|").replace("科学院·", "科学院|").replace("气象局-", "气象局|").replace("实验室-", "实验室|").replace("实验室·", "实验室|").replace("系统所-", "系统所|")
//                .replace("学部-", "学部|").replace("学系-", "学系|").replace("学院-", "学院|").replace("研究基地-", "研究基地|").replace("研究所-", "研究所|").replace("研究所·", "研究所|")
//                .replace("研究中心·", "研究中心|").replace("医学中心·", "医学中心|").replace("医院-", "医院|").replace("医院·", "医院|").replace("政府-", "政府|").replace("中国·", "中国|")
//                .replace(",", "|").replace(".", "|").replace(";", "|").replace(":", "|").replaceAll("\\s+(\\d+)", "$1").replaceAll("\\d{6}", "|").replaceAll("\\d{5}", "|")
//                .replace("，", "|").replace("。", "|").replace("；", "|").replace("：", "|").split("\\|");
//        for (String string : split) {
//            String s = string.trim();
//            if (s.length() > 1) {
//                String univ = findCnUnit(univList, s, errCnUnitSet);
//                String hosp = findCnUnit(hospList, s, errCnUnitSet);
//                if (!univ.isEmpty() && !hosp.isEmpty()) {
//                    unitSet.add(univ);
//                    unitSet.add(hosp);
//                    // 此类为大学加医院的组合体，分别加入大学和医院两部分
//                } else if (!univ.isEmpty()) {
//                    unitSet.add(univ);
//                } else if (!hosp.isEmpty()) {
//                    unitSet.add(hosp);
//                }
//            }
//        }
//    }

    public static List<String> getCsCdUnitListDept(String info) {
//        if (!Utils.isContainChinese(info)) {
//            Set<String> oneC1 = ReadUnit.getOneC1Dept(info, "cn", wordAndStandardMap, enDeptSet, deptAndSumMap);
//            return new ArrayList<>(oneC1);
//        }
        Map<String, Integer> deptAndSumMap = new HashMap<>();
        Map<Set<String>, Integer> ordinaryAndSumMap = new HashMap<>();
        List<String> univList = cList.getCnUnivList();
        List<String> hospList = cList.getCnHospList();
        Set<String> errCnUnitSet = cList.getCnErrUnitSet();
        Set<String> ordinarySet = new HashSet<>();
        // .replaceAll("\\s+(\\d+)", "$1") ->数字前面如果有空格则给予删除  四川大学华西第二医院/华西妇产儿童医院(.replace("/", "|")去掉的原因)
        String[] split = info.replace("|", " ").replace("　", " ").replace("（", "(").replace("）", ")").replaceAll(" +", " ").replace(" (", "(").replace(" )", ")")
                .replace("、", "|").replace("—", "|").replace("!", "|").replace("！", "|").replace("暨", "|")
                .replace("癌症中心·", "癌症中心|").replace("办公室-", "办公室|").replace("保健院·", "保健院|").replace("单位-", "单位|").replace("防治中心·", "防治中心|").replace("港航-", "港航|")
                .replace("规划署-", "规划署|").replace("科学院·", "科学院|").replace("气象局-", "气象局|").replace("实验室-", "实验室|").replace("实验室·", "实验室|").replace("系统所-", "系统所|")
                .replace("学部-", "学部|").replace("学系-", "学系|").replace("学院-", "学院|").replace("研究基地-", "研究基地|").replace("研究所-", "研究所|").replace("研究所·", "研究所|")
                .replace("研究中心·", "研究中心|").replace("医学中心·", "医学中心|").replace("医院-", "医院|").replace("医院·", "医院|").replace("政府-", "政府|").replace("中国·", "中国|")
                .replace(",", "|").replace(".", "|").replace(";", "|").replace(":", "|").replaceAll("\\s+(\\d+)", "$1").replaceAll("\\d{6}", "|").replaceAll("\\d{5}", "|")
                .replace("，", "|").replace("。", "|").replace("；", "|").replace("：", "|").split("\\|");
        for (String string : split) {
            String s = string.trim();
            if (s.length() > 1) {
                String univ = findCnUnit(univList, s, errCnUnitSet);
                String hosp = findCnUnit(hospList, s, errCnUnitSet);

                if (!univ.isEmpty() && !hosp.isEmpty()) {
                    ordinarySet.add(univ);
                    ordinarySet.add(hosp);
                    // 此类为大学加医院的组合体，分别加入大学和医院两部分
                    putDeptAndSumMap(hosp, s, deptAndSumMap);
                } else if (!univ.isEmpty()) {
                    ordinarySet.add(univ);
                    putDeptAndSumMap(univ, s, deptAndSumMap);
                } else if (!hosp.isEmpty()) {
                    ordinarySet.add(hosp);
                    putDeptAndSumMap(hosp, s, deptAndSumMap);
                } else {
                    deptAndSumMap.compute(s, (key, value) -> value != null ? (value + 1) : 1);
                }
            }
        }
        List<String> ordinaryList = new ArrayList<>(ordinarySet);
        if (!ordinarySet.isEmpty()) {
            ordinaryAndSumMap.compute(ordinarySet, (key, value) -> value != null ? (value + 1) : 1);
        }
        return ordinaryList;
    }

    public static void putDeptAndSumMap(String hosp, String s, Map<String, Integer> deptAndSumMap) {
        if (s.contains(hosp) && s.length() > hosp.length()) {
            String dept = s.substring(s.indexOf(hosp) + hosp.length());
            if (dept.length() > 1) {
                deptAndSumMap.compute(dept, (key, value) -> value != null ? (value + 1) : 1);
            }
        }
    }

    public static String findCnUnit(List<String> unitList, String str, Set<String> errUnitSet) {
        if ("中国科学院".equals(str)) {
            return "中国科学院";
        }
        for (String unit : unitList) {
            if (str.contains(unit) && !str.equals(unit)) {
                // lastIndexOf:四川大学华西第二医院/华西妇产儿童医院
                String trim = str.substring(0, str.lastIndexOf(unit) + unit.length());
                // 信阳师范学院商学院
                if (str.contains("学院")) {
                    trim = str.substring(0, str.indexOf(unit) + unit.length());
                }
                String end = str.substring(trim.length());
                // 中国石油大学(华东)信息与控制工程学院    ||    电网智能化调度与控制教育部重点实验室(山东大学)
                if ((end.startsWith("(") && end.contains(")")) || end.startsWith(")")) {
                    trim += end.substring(0, end.indexOf(")") + ")".length());
                }
                end = str.substring(trim.length());
                // 东部战区总医院临床药学科(南京大学医学院附属金陵医院)
                if (trim.contains("(") && (trim.endsWith("大学") || trim.endsWith("学院")) && end.contains(")")) {
                    trim += end.substring(0, end.indexOf(")") + ")".length());
                }
                trim = trim.startsWith("《") ? trim.substring("《".length()) : trim;
                trim = (errUnitSet.contains(trim.trim()) || trim.trim().length() < 4) ? "" : trim;
//                System.out.println("unit:" + unit + " str:" + str+ " trim:" + trim);
                return trim.length() < 255 ? trim.trim() : "";
            }
        }
        return "";
    }

    public static boolean isAbbrUniv(String t) {
        return "AHMU".equals(t) || "ANU".equals(t) || "BHU".equals(t) || "BMC".equals(t) || "BMSB".equals(t) || "CALTECH".equals(t) || "CAS".equals(t) || "CIT".equals(t) || "CITIID".equals(t) || "CMU".equals(t) || "CONICET".equals(t) || "CUHK".equals(t) || "CUNY".equals(t) ||
                "CWRU".equals(t) || "ECNU".equals(t) || "ENS".equals(t) || "ENSCL".equals(t) || "EPFL".equals(t) || "ESPCI".equals(t) || "ETH".equals(t) || "ETHZ".equals(t) || "FAU".equals(t) || "FDU".equals(t) || "FHNW".equals(t) || "GWU".equals(t) || "HIT".equals(t) ||
                "HKBU".equals(t) || "HKU".equals(t) || "HKUST".equals(t) || "HSE".equals(t) || "HUST".equals(t) || "IPN".equals(t) || "ISI".equals(t) || "IST".equals(t) || "ISU".equals(t) || "ITMO".equals(t) || "JHU".equals(t) || "KAIST".equals(t) || "KAUST".equals(t) ||
                "KIT".equals(t) || "KTH".equals(t) || "LMU".equals(t) || "LSE".equals(t) || "MHC".equals(t) || "MIT".equals(t) || "MSU".equals(t) || "NIBSC".equals(t) || "NTNU".equals(t) || "NTU".equals(t) || "NUHS".equals(t) || "NUS".equals(t) || "NWU".equals(t) ||
                "NYIT".equals(t) || "NYU".equals(t) || "OHSU".equals(t) || "OSU".equals(t) || "PKU".equals(t) || "POSTECH".equals(t) || "PSU".equals(t) || "RCSI".equals(t) || "RMIT".equals(t) || "RTP".equals(t) || "RWTH".equals(t) || "SFU".equals(t) || "SJTU".equals(t) ||
                "SMU".equals(t) || "SPBU".equals(t) || "SRUC".equals(t) || "STU".equals(t) || "SUNY".equals(t) || "TCM".equals(t) || "THU".equals(t) || "TMD".equals(t) || "TSU".equals(t) || "TUM".equals(t) || "UBC".equals(t) || "UCB".equals(t) || "UCI".equals(t) ||
                "UCL".equals(t) || "UCLA".equals(t) || "UCM".equals(t) || "UCSC".equals(t) || "UCSD".equals(t) || "UCSF".equals(t) || "UERJ".equals(t) || "UFC".equals(t) || "UFF".equals(t) || "UFG".equals(t) || "UFMG".equals(t) || "UFPB".equals(t) || "UFPE".equals(t) ||
                "UFRGS".equals(t) || "UFSC".equals(t) || "UFU".equals(t) || "UFV".equals(t) || "UGA".equals(t) || "UIC".equals(t) || "UIUC".equals(t) || "UMBC".equals(t) || "UMKC".equals(t) || "UML".equals(t) || "UMN".equals(t) || "UNAM".equals(t) || "UNC".equals(t) ||
                "UNESP".equals(t) || "UNICAEN".equals(t) || "UNIROUEN".equals(t) || "UNL".equals(t) || "UNLV".equals(t) || "UNM".equals(t) || "UNSW".equals(t) || "UOC".equals(t) || "UPMC".equals(t) || "USC".equals(t) || "USD".equals(t) || "USF".equals(t) || "USM".equals(t) ||
                "USTC".equals(t) || "UTD".equals(t) || "UTK".equals(t) || "UTM".equals(t) || "UVSQ".equals(t) || "VCU".equals(t) || "VIA".equals(t) || "VUB".equals(t) || "WSU".equals(t) || "WUSTL".equals(t);
    }

    public static Set<String> getEnUnit(String full, Map<String, String> standardMap, Set<String> acadSet, Set<String> centerSet, Set<String> collSet, Set<String> hospSet, Set<String> univSet, Set<String> removeSet, Set<String> dellSet) {
        // 拆分C1的核心代码，优先顺序是大学加医院，科学院加医院，医学院加医院，医院加其它单词，
        String[] split = full.replace(". ", ", ").replace(";", ",").split(",");
        Set<String> univs = new HashSet<>();
        Set<String> acads = new HashSet<>();
        Set<String> colls = new HashSet<>();
        Set<String> hosps = new HashSet<>();
        Set<String> centers = new HashSet<>();
        Set<String> others = new HashSet<>();
        others.add(full);
        Set<String> collegeSet = new HashSet<>();
        for (String t : split) {
            t = t.trim();
            if (!t.isEmpty()) {
                String s = t.toLowerCase();
                if (standardMap.containsKey(s)) {
                    t = standardMap.get(s);
                } else {
                    standardMap.put(s, t);
                }
                if (isAbbrUniv(t)) {
                    univs.add(t);
                }
                if (t.contains("[ĺ") && t.contains("ļ]")) {
                    // 提取以"[ĺ"开头，以"ļ]"结尾的字符串。
                    String edu = "[@" + t.replaceAll("(.+?)\\[ĺ(.+?)ļ](.+?$)", "$2") + "]";
                    univs.add(edu);
                }

                if (s.endsWith(" college")) {
                    collegeSet.add(t);
                }
                boolean univ = isUniv(s, univSet, removeSet);
                boolean hosp = isHosp(s, hospSet, removeSet);
                boolean coll = isColl(s, collSet, removeSet);
                boolean academy = isAcad(s, acadSet, removeSet);
                boolean center = isCenter(s, centerSet, removeSet);
                if (univ && hosp) {
                    univs.add(t);
                    hosps.add(t);
                } else if (coll && hosp) {
                    colls.add(t);
                    hosps.add(t);
                } else if (academy && hosp) {
                    acads.add(t);
                    hosps.add(t);
                } else if (univ && center) {
                    univs.add(t);
                    hosps.add(t);
                } else if (coll && center) {
                    colls.add(t);
                    hosps.add(t);
                } else if (academy && center) {
                    acads.add(t);
                    hosps.add(t);
                } else if (univ) {
                    univs.add(t);
                } else if (academy) {
                    acads.add(t);
                } else if (coll) {
                    colls.add(t);
                } else if (hosp) {
                    hosps.add(t);
                } else if (center) {
                    centers.add(t);
                }
            }
        }
//        System.out.println("univs: " + univs);
//        System.out.println("hosps: " + hosps);
//        System.out.println("colls: " + colls);
//        System.out.println("acads: " + acads);
//        System.out.println("centers: " + centers);
//        System.out.println("others: " + others);
        String c = full.toLowerCase();
        String u = (CollUtil.join(univs, "; ") + "; " + CollUtil.join(hosps, "; ")).toLowerCase();
        // 济南大学||暨南大学
        if (u.contains("jinan univ") || u.contains("jinan university")) {
            if ((c.contains("guangdong") || c.contains("guang dong")) && !u.contains("guangdong") && !u.contains("guang dong")) {
                univs.add("Guangdong");
            } else if ((c.contains("guangzhou") || c.contains("guang zhou")) && !u.contains("guangzhou") && !u.contains("guang zhou")) {
                univs.add("Guangzhou");
            } else if ((c.contains("shenzhen") || c.contains("shen zhen")) && !u.contains("shenzhen") && !u.contains("shen zhen")) {
                univs.add("Shenzhen");
            } else if ((c.contains("zhuhai") || c.contains("zhu hai")) && !u.contains("zhuhai") && !u.contains("zhu hai")) {
                univs.add("Zhuhai");
            } else if ((c.contains("shunde") || c.contains("shun de")) && !u.contains("shunde") && !u.contains("shun de")) {
                univs.add("Shunde");
            } else if ((c.contains("baoan") || c.contains("bao an")) && !u.contains("baoan") && !u.contains("bao an")) {
                univs.add("Baoan");
            } else if ((c.contains("heyuan") || c.contains("he yuan")) && !u.contains("heyuan") && !u.contains("he yuan")) {
                univs.add("Heyuan");
            } else if ((c.contains("dongguan") || c.contains("dong guan")) && !u.contains("dongguan") && !u.contains("dong guan")) {
                univs.add("Dongguan");
            } else if ((c.contains("shandong") || c.contains("shan dong") || c.contains("250022")) && !u.contains("shandong") && !u.contains("shan dong") && !u.contains("250022")) {
                univs.add("Shandong");
            }
            // 中国石油大学(华东)||中国石油大学(北京)
        } else if ((u.contains("china univ petr") || u.contains("univ petr") || u.contains("petr univ") || u.contains("china university petroleum") || u.contains("university petroleum") || u.contains("petroleum university")) && !u.contains("china univ petr east") && !u.contains("china univ petr huadong") && !u.contains("china univ petr beijing")) {
            if ((c.contains("shandong") || c.contains("shan dong") || c.contains("qingdao") || c.contains("qing dao") || c.contains("dongying") || c.contains("dong ying") || c.contains("huadong") || c.contains("hua dong")) && !u.contains("shandong") && !u.contains("shan dong") && !u.contains("qingdao") && !u.contains("qing dao") && !u.contains("dongying") && !u.contains("dong ying") && !u.contains("huadong") && !u.contains("hua dong")) {
                univs.add("East China");
            } else if ((c.contains("beijing") || c.contains("bei jing") || c.contains("karamay") || c.contains("xinjiang") || c.contains("xin jiang")) && !u.contains("beijing") && !u.contains("bei jing") && !u.contains("karamay") && !u.contains("xinjiang") && !u.contains("xin jiang")) {
                univs.add("Beijing");
            }
            // 中国地质大学(武汉)||中国地质大学(北京)
        } else if (u.contains("china univ geosci") || u.contains("chinese univ geosci") || u.contains("china university geosci") || u.contains("chinese university geosci")) {
            if ((c.contains("wuhan") || c.contains("wu han") || c.contains("hubei") || c.contains("hu bei")) && !u.contains("wuhan") && !u.contains("wu han") && !u.contains("hubei") && !u.contains("hu bei")) {
                univs.add("Wuhan");
            } else if ((c.contains("beijing") || c.contains("bei jing")) && !u.contains("beijing") && !u.contains("bei jing")) {
                univs.add("Beijing");
            }
            // 中国矿业大学(徐州)||中国矿业大学(北京)
        } else if (u.contains("china univ min & technol") || u.contains("china univ mining & technol") || u.contains("china univ min technol") || u.contains("chinese univ min & technol") || u.contains("china univ min & tech") || u.contains("china univ ming & technol") || u.contains("china university mining & technol") || u.contains("china university mining technol")) {
            if ((c.contains("jiangsu") || c.contains("jiang su") || c.contains("xuzhou") || c.contains("xu zhou")) && !u.contains("jiangsu") && !u.contains("jiang su") && (!u.contains("xuzhou") && !u.contains("xu zhou"))) {
                univs.add("Xuzhou");
            } else if ((c.contains("beijing") || c.contains("bei jing")) && (!u.contains("beijing") && !u.contains("bei jing"))) {
                univs.add("Beijing");
            }
            // 中国医科大学||中国医药大学
        } else if (u.contains("china med univ") || u.contains("china med coll") || u.contains("china medical university")) {
            if ((c.contains("shenyang") || c.contains("shen yang") || c.contains("liaoning") || c.contains("liao ning")) && !u.contains("shenyang") && !u.contains("shen yang") && (!u.contains("liaoning") && !u.contains("liao ning"))) {
                univs.add("Shenyang");
            } else if ((c.contains("taiwan") || c.contains("tai wan") || c.contains("taichung") || c.contains("tai chung")) && !u.contains("taiwan") && !u.contains("tai wan") && !u.contains("taichung") && !u.contains("tai chung")) {
                univs.add("Taiwan");
            }
            // 北京协和医院||华中科技大学同济医学院附属协和医院||福建医科大学附属协和医院
        } else if (u.contains("union hosp") || u.contains("xiehe hosp") || u.contains("union hospital") || u.contains("xiehe hospital")) {
            if ((c.contains("peking") || c.contains("beijing") || c.contains("bei jing") || c.contains("chinese acad med sci")) && !u.contains("peking") && !u.contains("beijing") && (!u.contains("bei jing") && !u.contains("chinese acad med sci"))) {
                hosps.add("Beijing");
            } else if ((c.contains("huazhong") || c.contains("tongji") || c.contains("hubei") || c.contains("wuhan")) && !u.contains("huazhong") && !u.contains("tongji") && !u.contains("hubei") && !u.contains("wuhan")) {
                hosps.add("Wuhan");
            } else if ((c.contains("fujian") || c.contains("fu jian") || c.contains("fuzhou") || c.contains("fu zhou")) && !u.contains("fujian") && !u.contains("fu jian") && !u.contains("fuzhou") && !u.contains("fu zhou")) {
                hosps.add("Fuzhou");
            }
            // 复旦大学附属中山医院||大连大学附属中山医院||浙江省中山医院||厦门大学附属中山医院||湖北省中山医院||重庆市中山医院
        } else if (u.contains("zhongshan hosp") || u.contains("zhong shan hosp") || u.contains("sun yat sen hosp") || u.contains("zhongshan hospital") || u.contains("zhong shan hospital") || u.contains("sun yat sen hospital")) {
            if ((c.contains("dalian") || c.contains("da lian") || c.contains("liaoning") || c.contains("liao ning")) && !u.contains("dalian") && !u.contains("da lian") && (!u.contains("liaoning") && !u.contains("liao ning"))) {
                hosps.add("Dalian");
            } else if ((c.contains("fudan") || c.contains("fu dan") || c.contains("shanghai") || c.contains("shang hai")) && !u.contains("fudan") && !u.contains("fu dan") && !u.contains("shanghai") && !u.contains("shang hai")) {
                hosps.add("Shanghai");
            } else if ((c.contains("xiamen") || c.contains("xia men") || c.contains("fujian") || c.contains("fu jian")) && !u.contains("fujian") && !u.contains("fu jian") && !u.contains("xiamen") && !u.contains("xia men") && !u.contains("fudan") && !u.contains("fu dan")) {
                hosps.add("Xiamen");
            } else if ((c.contains("hubei") || c.contains("hu bei") || c.contains("wuhan") || c.contains("wu han")) && !u.contains("hubei") && !u.contains("hu bei") && !u.contains("wuhan") && !u.contains("wu han")) {
                hosps.add("Hubei");
            } else if ((c.contains("chongqing") || c.contains("chong qing")) && !u.contains("chongqing") && !u.contains("chong qing")) {
                hosps.add("Chongqing");
            }
            // 中日友好医院||吉林大学中日联谊医院
        } else if ((u.contains("china") || u.contains("chinese")) && (u.contains("japan") || u.contains("japanese")) && (u.contains("friendship") || u.contains("union")) && (u.contains(" hosp") || u.contains(" hospital"))) {
            if ((c.contains("beijing") || c.contains("bei jing")) && !u.contains("beijing") && !u.contains("bei jing")) {
                hosps.add("Beijing");
            } else if ((c.contains("jilin") || c.contains("ji lin") || c.contains("bethune") || c.contains("norman")) && !u.contains("jilin") && !u.contains("ji lin") && !u.contains("bethune") && !u.contains("norman")) {
                hosps.add("Jilin");
            }
            // 中国东北大学||美国东北大学
        } else if (u.contains("northeastern univ") || u.contains("northeast univ") || u.contains("north eastern univ") || u.contains("north east univ") || u.contains("northestern univ") || u.contains(" ne univ") || u.startsWith("ne univ") || u.contains("northeastern university")) {
            if ((c.contains("shenyang") || c.contains("shen yang") || c.contains("qinhuangdao") || c.contains("qin huang dao") || c.contains("liaoning") || c.contains("liao ning") || c.contains("china") || c.contains("chinese")) && !u.contains("shenyang") && !u.contains("shen yang") && !u.contains("qinhuangdao") && !u.contains("qin huang dao") && !u.contains("liaoning") && !u.contains("liao ning") && !u.contains("china") && !u.contains("chinese")) {
                univs.add("China");
            } else if ((c.contains("boston") || c.contains(" usa") || c.contains("ma 02115")) && !u.contains("boston") && !u.contains(" usa") && !u.contains("ma 02115")) {
                univs.add("USA");
            }
            // 国立清华大学||清华大学
        } else if (u.contains("tsing hua univ") || u.contains("tsinghua univ") || u.contains("qinghua univ") || u.contains("qing hua univ") || u.contains("tsing hua university") || u.contains("tsinghua university") || u.contains("qinghua university") || u.contains("qing hua university")) {
            if ((c.contains("taiwan") || c.contains("tai wan") || c.contains("hsinchu")) && !u.contains("taiwan") && !u.contains("tai wan") && !u.contains("hsinchu")) {
                univs.add("Taiwan");
            } else if ((c.contains("beijing") || c.contains("bei jing") || c.contains("100084")) && !u.contains("beijing") && !u.contains("bei jing") && !u.contains("100084")) {
                univs.add("Beijing");
            }
            // 国立中山大学||中山大学
        } else if (u.contains("sun yat sen univ") || u.contains("zhongshan univ") || u.contains("sun yet sen univ") || u.contains("sun yatsen univ") || u.contains("zhong shan univ") || u.contains("sun yet san univ") || u.contains("sun yat sen university") || u.contains("zhongshan university") || u.contains("sun yet sen university") || u.contains("sun yatsen university") || u.contains("zhong shan university") || u.contains("sun yet san university")) {
            if ((c.contains("taiwan") || c.contains("tai wan") || c.contains("hsinchu") || c.contains("kaohsiung") || c.contains("taipei") || c.contains("tai pei")) && !u.contains("taiwan") && !u.contains("tai wan") && !u.contains("hsinchu") && !u.contains("kaohsiung") && !u.contains("taipei") && !u.contains("tai pei")) {
                univs.add("Taiwan");
            } else if ((c.contains("guangdong") || c.contains("guang dong") || c.contains("shenzhen") || c.contains("shen zhen") || c.contains("guangzhou") || c.contains("guang zhou")) && !u.contains("guangdong") && !u.contains("guang dong") && !u.contains("shenzhen") && !u.contains("shen zhen") && !u.contains("guangzhou") && !u.contains("guang zhou")) {
                if (!"sun yat sen university".equals(u)) {
                    univs.add("Guangdong");
                }
            }
            // 国立东华大学||东华大学
        } else if (u.contains("dong hwa univ") || u.contains("donghua univ") || u.contains("dong hua univ") || u.contains("univ donghua") || u.contains("doughua univ") || u.contains("dong hwa university") || u.contains("donghua university") || u.contains("dong hua university") || u.contains("university donghua") || u.contains("doughua university")) {
            if ((c.contains("taiwan") || c.contains("tai wan") || c.contains("hualien")) && !u.contains("taiwan") && !u.contains("tai wan") && !u.contains("hualien")) {
                univs.add("Taiwan");
            } else if ((c.contains("china text univ") || c.contains("china textile univ") || c.contains("china text university") || c.contains("china textile university") || c.contains("shanghai") || c.contains("shang hai") || c.contains("201620")) && !u.contains("china text univ") && !u.contains("china textile univ") && !u.contains("china text university") && !u.contains("china textile university") && !u.contains("shanghai") && !u.contains("shang hai") && !u.contains("201620")) {
                univs.add("Shanghai");
            }
        }

        // Jiaxing College, Affiliated Hospital 1, Medical Center Anesthesia & Pain, Jiaxing, People's Republic of China 处理一些学院的附属医院问题
        if (!collegeSet.isEmpty() && univs.isEmpty() && colls.isEmpty() && !hosps.isEmpty()) {
            boolean bool = false;
            for (String hosp : hosps) {
                if (hosp.toLowerCase().contains("affiliated hospital")) {
                    bool = true;
                    break;
                }
            }
            if (bool) {
                return addAllSet(collegeSet, hosps);
            }
        }

        univs = dellEduMail(univs);
        if (hosps.isEmpty()) {
//            System.out.println("1.");
            if (centers.isEmpty()) {
//                System.out.println("3.");
                if (!univs.isEmpty() && notDell(univs, dellSet)) {
                    return univs;
                } else if (!colls.isEmpty() && notDell(colls, dellSet)) {
                    return colls;
                } else if (!acads.isEmpty() && notDell(acads, dellSet)) {
                    return acads;
                } else {
                    return others;
                }
            } else {
//                System.out.println("4.");
                Set<String> uSet = addAllSet(centers, univs);
                Set<String> cSet = addAllSet(centers, colls);
                Set<String> aSet = addAllSet(centers, acads);
                if (!univs.isEmpty() && notDell(uSet, dellSet)) {
                    return uSet;
                } else if (!colls.isEmpty() && notDell(cSet, dellSet)) {
                    return cSet;
                } else if (!acads.isEmpty() && notDell(aSet, dellSet)) {
                    return aSet;
                } else {
                    return centers;
                }
            }
        } else {
//            System.out.println("2.");
            Set<String> uSet = addAllSet(hosps, univs);
            Set<String> cSet = addAllSet(hosps, colls);
            Set<String> aSet = addAllSet(hosps, acads);
            Set<String> hSet = addAllSet(hosps, centers);
            if (!univs.isEmpty() && notDell(uSet, dellSet)) {
                return uSet;
            } else if (!colls.isEmpty() && notDell(cSet, dellSet)) {
                return cSet;
            } else if (!acads.isEmpty() && notDell(aSet, dellSet)) {
                return aSet;
            } else if (!centers.isEmpty() && notDell(hSet, dellSet)) {
                return hSet;
            } else if (notDell(hosps, dellSet)) {
                return hosps;
            } else {
                return others;
            }
        }
    }

    public static Set<String> dellEduMail(Set<String> univs) {
        // 如果大学集合内，同时包括"[@"和"university"，则取消"[@"。为什么？因为在PubMed的作者单位信息中，往往会包含作者的edu邮箱，利用此邮箱可以区分作者的单位，但是在单位信息中已经包含了"university"时，就没有必要保留两份了。
        if (univs.size() > 1) {
            boolean bool = false;
            for (String univ : univs) {
                if (univ.contains("university")) {
                    bool = true;
                    break;
                }
            }
            if (bool) {
                Set<String> result = new HashSet<>();
                for (String univ : univs) {
                    if (!univ.startsWith("[@")) {
                        result.add(univ);
                    }
                }
                return result;
            }
        }
        return univs;
    }

    public static boolean notDell(Set<String> aSet, Set<String> dellSet) {
        // 如果指定集合中，全部是dellSet中的值，则不必要加入。如果有任一个不是，则可以加入。
        for (String s : aSet) {
            if (!dellSet.contains(s.toLowerCase().trim())) {
                return true;
            }
        }
        return false;
    }

//    @Data
//    public static class C1Split implements Serializable {
//        private static final long serialVersionUID = 1L;
//        private Set<String> univSet = new HashSet<>();
//        private Set<String> academySet = new HashSet<>();
//        private Set<String> collSet = new HashSet<>();
//        private Set<String> hospSet = new HashSet<>();
//        private Set<String> otherSet = new HashSet<>();
//    }

    public static Set<String> addAllSet(Set<String> aSet, Set<String> bSet) {
        Set<String> set = new HashSet<>();
        set.addAll(aSet);
        set.addAll(bSet);
        return set;
    }

//    public static Set<String> getDept(String full, Set<String> deptSet, Set<String> dellSet, Map<String, String> standardMap) {
//        String[] split = full.replace("；", ", ").replace("： ", ", ").replace("。", ", ").replace("， ", ", ")
//                .replace("; ", ", ").replace(": ", ", ").replace(". ", ", ").split(", ");
//        Set<String> result = new HashSet<>();
//        for (String t : split) {
//            String s = t.toLowerCase().trim();
//            if (standardMap.containsKey(s)) {
//                t = standardMap.get(s);
//            } else {
//                standardMap.put(s, t);
//            }
//            if (!dellSet.contains(s)) {
//                if (deptSet.contains(s)) {
//                    result.add(t);
//                }
//                for (String d : deptSet) {
//                    if (s.contains(d)) {
//                        result.add(t);
//                    }
//                }
//                String e = " " + s + " ";
//                if (e.contains(" branch ") || e.contains(" center ") || e.contains(" centre ") || e.contains(" clinic ") || e.contains(" deparment ") || e.contains(" departament ") || e.contains(" departamento ") || e.contains(" departement ") ||
//                        e.contains(" département ") || e.contains(" department ") || e.contains(" departments ") || e.contains(" dermatologie ") || e.contains(" division ") || e.contains(" gynaecology ") || e.contains(" institute ") ||
//                        e.contains(" institution ") || e.contains(" neuroscience ") || e.contains(" neurosurgery ") || e.contains(" section ") || e.contains(" service ") || e.contains(" surgery ") || e.contains(" unit ") || e.contains(" ward ")) {
//                    result.add(t);
//                }
//            }
//        }
//        if (result.size() > 1) {
//            result.remove("Department Medical");
//        }
//        return result;
//    }

    public static boolean isCenter(String s, Set<String> centerSet, Set<String> removeSet) {
        if (removeSet.contains(s)) {
            return false;
        }
        // 包含各种医学中心、癌症中心的写法
        if (centerSet.contains(s)) {
            return true;
        }
        for (String t : centerSet) {
            if (s.contains(t)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isUniv(String full, Set<String> univSet, Set<String> removeSet) {
        // 包含各种大学的写法，2025.02.22 版本
        String s = full.toLowerCase().trim();
        if (removeSet.contains(s)) {
            return false;
        }
        if (univSet.contains(s)) {
            return true;
        }
        for (String u : univSet) {
            if (s.contains(u)) {
                return true;
            }
        }
        if (Pattern.compile("[a-z]").matcher(full).find()) {
            String f = full.trim();
            if (isContainsAbbr(full)) {
                return true;
            }
        }
        String l = " " + s + " ";
        return l.contains(" 1univ ") || l.contains(" 2univ ") || l.contains(" 3univ ") || l.contains(" 4univ ") || l.contains(" 5univ ") || l.contains(" 6univ ") || l.contains(" 7univ ") || l.contains(" 8univ ") || l.contains(" 9univ ") || l.contains(" jilinuniv ") ||
                l.contains(" univ ") || l.contains(" univ. ") || l.contains(" univ1 ") || l.contains(" univ2 ") || l.contains(" univap ") || l.contains(" univeris ") || l.contains(" univers ") || l.contains(" universe ") || l.contains(" universi ") || l.contains(" universit ") ||
                l.contains(" universiy ") || l.contains(" universty ") || l.contains(" univiers ") || l.contains(" unversity ") || l.contains(" univesity ") || l.contains(" universal ") || l.contains(" univille ") || l.contains(" universo ") || l.contains(" univates ") ||
                l.contains(" univstr ") || l.contains(" univmed ") || l.contains(" univasf ") || l.contains(" univali ") || l.contains(" univpm ") || l.contains(" univer ") || l.contains(" unive ");
    }

    public static boolean isContainsAbbr(String full) {
        String f = full.trim();
        if (Pattern.compile("[a-z]").matcher(f).find()) {
            return f.contains("AHMU") || f.contains("ANU") || f.contains("BHU") || f.contains("BMC") || f.contains("BMSB") || f.contains("CALTECH") || f.contains("CAS") || f.contains("CIT") || f.contains("CITIID") || f.contains("CMU") || f.contains("CONICET") || f.contains("CUHK") ||
                    f.contains("CUNY") || f.contains("CWRU") || f.contains("ECNU") || f.contains("ENS") || f.contains("ENSCL") || f.contains("EPFL") || f.contains("ESPCI") || f.contains("ETH") || f.contains("ETHZ") || f.contains("FAU") || f.contains("FDU") || f.contains("FHNW") ||
                    f.contains("GWU") || f.contains("HIT") || f.contains("HKBU") || f.contains("HKU") || f.contains("HKUST") || f.contains("HSE") || f.contains("HUST") || f.contains("IPN") || f.contains("ISI") || f.contains("IST") || f.contains("ISU") || f.contains("ITMO") ||
                    f.contains("JHU") || f.contains("KAIST") || f.contains("KAUST") || f.contains("KIT") || f.contains("KTH") || f.contains("LMU") || f.contains("LSE") || f.contains("MHC") || f.contains("MIT") || f.contains("MSU") || f.contains("NIBSC") || f.contains("NTNU") ||
                    f.contains("NTU") || f.contains("NUHS") || f.contains("NUS") || f.contains("NWU") || f.contains("NYIT") || f.contains("NYU") || f.contains("OHSU") || f.contains("OSU") || f.contains("PKU") || f.contains("POSTECH") || f.contains("PSU") || f.contains("RCSI") ||
                    f.contains("RMIT") || f.contains("RTP") || f.contains("RWTH") || f.contains("SFU") || f.contains("SJTU") || f.contains("SMU") || f.contains("SPBU") || f.contains("SRUC") || f.contains("STU") || f.contains("SUNY") || f.contains("TCM") || f.contains("THU") ||
                    f.contains("TMD") || f.contains("TSU") || f.contains("TUM") || f.contains("UBC") || f.contains("UCB") || f.contains("UCI") || f.contains("UCL") || f.contains("UCLA") || f.contains("UCM") || f.contains("UCSC") || f.contains("UCSD") || f.contains("UCSF") ||
                    f.contains("UERJ") || f.contains("UFC") || f.contains("UFF") || f.contains("UFG") || f.contains("UFMG") || f.contains("UFPB") || f.contains("UFPE") || f.contains("UFRGS") || f.contains("UFSC") || f.contains("UFU") || f.contains("UFV") || f.contains("UGA") ||
                    f.contains("UIC") || f.contains("UIUC") || f.contains("UMBC") || f.contains("UMKC") || f.contains("UML") || f.contains("UMN") || f.contains("UNAM") || f.contains("UNC") || f.contains("UNESP") || f.contains("UNICAEN") || f.contains("UNIROUEN") || f.contains("UNL") ||
                    f.contains("UNLV") || f.contains("UNM") || f.contains("UNSW") || f.contains("UOC") || f.contains("UPMC") || f.contains("USC") || f.contains("USD") || f.contains("USF") || f.contains("USM") || f.contains("USTC") || f.contains("UTD") || f.contains("UTK") ||
                    f.contains("UTM") || f.contains("UVSQ") || f.contains("VCU") || f.contains("VIA") || f.contains("VUB") || f.contains("WSU") || f.contains("WUSTL");
        }
        return false;
    }

    public static boolean isSubject(String full, Set<String> subjectSet, Set<String> removeSet) {
        // 此类包含了dept、center、coll、acad四类的合并，作为判断是否可以使用于学科的归属。2025.02.22 版本
        String s = full.toLowerCase().trim();
        if (removeSet.contains(s)) {
            return false;
        }
        if (subjectSet.contains(s)) {
            return true;
        }
        for (String t : subjectSet) {
            if (s.contains(t)) {
                return true;
            }
        }
        String l = " " + s + " ";
        return l.contains(" college ") || l.contains(" usda/ars ") || l.contains(" hall ") || l.contains(" section ") || l.contains(" ward ") || l.contains(" program ") || l.contains(" branch ") || l.contains(" faculty ") || l.contains(" division ") ||
                l.contains(" school ") || l.contains(" deparment ") || l.contains(" niaid/nih ") || l.contains(" academy ") || l.contains(" seminary ") || l.contains(" ars-usda ") || l.contains(" cea-cnrs ") || l.contains(" center ") || l.contains(" centre ") ||
                l.contains(" unit ") || l.contains(" insa-lyon ") || l.contains(" service ") || l.contains(" iata-csic ") || l.contains(" urology ") || l.contains(" institute ") || l.contains(" clinic ") || l.contains(" andrology ") || l.contains(" surgery ")||
                l.contains(" college,") || l.contains(" usda/ars,") || l.contains(" hall,") || l.contains(" section,") || l.contains(" ward,") || l.contains(" program,") || l.contains(" branch,") || l.contains(" faculty,") || l.contains(" division,") ||
                l.contains(" school,") || l.contains(" deparment,") || l.contains(" niaid/nih,") || l.contains(" academy,") || l.contains(" seminary,") || l.contains(" ars-usda,") || l.contains(" cea-cnrs,") || l.contains(" center,") || l.contains(" centre,") ||
                l.contains(" unit,") || l.contains(" insa-lyon,") || l.contains(" service,") || l.contains(" iata-csic,") || l.contains(" urology,") || l.contains(" institute,") || l.contains(" clinic,") || l.contains(" andrology,") || l.contains(" surgery,");
    }

    public static boolean isHosp(String full, Set<String> hospSet, Set<String> removeSet) {
        // 包含各种医院的写法，以及诊所的名称和医院的全名，2025.02.22 版本
        String s = full.toLowerCase().trim();
        if (removeSet.contains(s)) {
            return false;
        }
        if (hospSet.contains(s)) {
            return true;
        }
        for (String t : hospSet) {
            if (s.contains(t)) {
                return true;
            }
        }

        if (Pattern.compile("[a-z]").matcher(full).find()) {
            if (full.contains("MSKCC") || full.contains("MDACC") || full.contains("WRNMMC") || full.contains("INCOR")) {
                return true;
            }
        }
        String l = " " + s + " ";
        return l.contains(" &hosp ") || l.contains(" ,hosp ") || l.contains(" 101hosp ") || l.contains(" 301hosp ") || l.contains(" 302hosp ") || l.contains(" 401hosp ") || l.contains(" genhosp ") || l.contains(" hôpital ") || l.contains(" hosp ") ||
                l.contains(" hosp& ") || l.contains(" hosp1 ") || l.contains(" hosp1, ") || l.contains(" hosp1. ") || l.contains(" hosp10 ") || l.contains(" hosp2 ") || l.contains(" hosp3 ") || l.contains(" hospial ") || l.contains(" hospita ") ||
                l.contains(" hosptal ") || l.contains(" hosptial ") || l.contains(" hospices ") || l.contains(" hospice ") || l.contains(" hospi ") || l.contains(" hosp,") || l.contains(" &hosp,") || l.contains(" ,hosp,") || l.contains(" 101hosp,") ||
                l.contains(" 301hosp,") || l.contains(" 302hosp,") || l.contains(" 401hosp,") || l.contains(" genhosp,") || l.contains(" hôpital,") || l.contains(" hosp&,") || l.contains(" hosp1,") || l.contains(" hosp1,,") || l.contains(" hosp1.,") ||
                l.contains(" hosp10,") || l.contains(" hosp2,") || l.contains(" hosp3,") || l.contains(" hospial,") || l.contains(" hospita,") || l.contains(" hosptal,") || l.contains(" hosptial,") || l.contains(" hospices,") || l.contains(" hospice,") ||
                l.contains(" hospi,");
    }

    public static boolean isColl(String s, Set<String> collSet, Set<String> removeSet) {
        // 包含各种医学院的写法
        if (removeSet.contains(s)) {
            return false;
        }
        if (collSet.contains(s)) {
            return true;
        }
        for (String t : collSet) {
            if (s.contains(t)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isAcad(String s, Set<String> acadSet, Set<String> removeSet) {
        // acadSet包含了国家机构、公司名称以及中国科学院等
        if (removeSet.contains(s)) {
            return false;
        }
        if (acadSet.contains(s) || "us army".equals(s) || "usda ars".equals(s) || "u.s. fda".equals(s) || "u.s. epa".equals(s) || "nhs fife".equals(s) || "cea-cnrs".equals(s) || "usda/ars".equals(s) || "ars-usda".equals(s) || "niaid/nih".equals(s) || "chu lille".equals(s)
                || "iata-csic".equals(s) || "u.s. army".equals(s) || "petrochina".equals(s) || "met office".equals(s) || "in2p3-cnrs".equals(s) || "i.r.c.c.s.".equals(s) || "cnrs/in2p3".equals(s) || "cemes-cnrs".equals(s) || "dementia uk".equals(s) || "us oncology".equals(s)
                || "ausl modena".equals(s) || "nhs england".equals(s) || "nih bethesda".equals(s) || "ibm research".equals(s) || "state tb cell".equals(s) || "haryana india".equals(s) || "ukk institute".equals(s) || "chatham house".equals(s) || "gujarat india".equals(s)
                || "nsw australia".equals(s) || "health canada".equals(s) || "irccs neuromed".equals(s) || "eurac research".equals(s) || "country office".equals(s) || "ontario health".equals(s) || "national heart".equals(s) || "ivi foundation".equals(s)) {
            return true;
        }
        for (String t : acadSet) {
            if (s.contains(t)) {
                return true;
            }
        }
        return false;
    }


}
